home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_005 / joystick / joystick.c < prev   
C/C++ Source or Header  |  1992-05-06  |  8KB  |  293 lines

  1. /*
  2.  *
  3.  *    DISCLAIMER:
  4.  *
  5.  *    This program is provided as a service to the programmer
  6.  *    community to demonstrate one or more features of the Amiga
  7.  *    personal computer.  These code samples may be freely used
  8.  *    for commercial or noncommercial purposes.
  9.  * 
  10.  *     Commodore Electronics, Ltd ("Commodore") makes no
  11.  *    warranties, either expressed or implied, with respect
  12.  *    to the program described herein, its quality, performance,
  13.  *    merchantability, or fitness for any particular purpose.
  14.  *    This program is provided "as is" and the entire risk
  15.  *    as to its quality and performance is with the user.
  16.  *    Should the program prove defective following its
  17.  *    purchase, the user (and not the creator of the program,
  18.  *    Commodore, their distributors or their retailers)
  19.  *    assumes the entire cost of all necessary damages.  In 
  20.  *    no event will Commodore be liable for direct, indirect,
  21.  *    incidental or consequential damages resulting from any
  22.  *    defect in the program even if it has been advised of the 
  23.  *    possibility of such damages.  Some laws do not allow
  24.  *    the exclusion or limitation of implied warranties or
  25.  *    liabilities for incidental or consequential damages,
  26.  *    so the above limitation or exclusion may not apply.
  27.  *
  28.  */
  29.  
  30. /* joystick.c, shows how to read a joystick on the Amiga */
  31.  
  32.  
  33. /* *********************************************************************** */
  34. /* joystick test, for right game port on the Amiga.
  35.  
  36.    Notes:  The right port is used for this test because the input.device
  37.    task is busy continuously with the lefthand port, feeding input events
  38.    to intuition or console devices.  If Intuition is not activated
  39.    (applications which take over the whole machine may decide not to
  40.    activate Intuition), and if no console.device is activated either,
  41.    the input.device will never activate... allowing the application
  42.    free reign to use either the left OR the right hand joystick/mouse
  43.    port.  If either intuition or the console device are activated,
  44.    the lefthand port will yield, at best, every alternate input
  45.    event to an external application such as this test program.
  46.    This will undoubtedly mess up either of the two applications
  47.    and should therefore be avoided.  It was ok to use the right
  48.    port in this case, since the system has no particular interest
  49.    in monitoring it.
  50.  
  51.    Author:  Rob Peck, 12/1/85
  52.  
  53.    This code may be freely utilized in developing programs for the Amiga.
  54.  
  55. *********************************************************************** */
  56.  
  57.  
  58. #include <exec/types.h>
  59. #include <exec/devices.h>
  60. #include <graphics/gfx.h>
  61. #include <devices/gameport.h>
  62. #include <devices/inputevent.h>
  63.  
  64. LONG GfxBase=0;
  65.  
  66. #define XMOVE 10
  67. #define YMOVE 10
  68. #define MAX(m,n) (m > n ? m : n)
  69. #define FOREVER for(;;)
  70. struct InputEvent *game_data;    /* pointer into the returned data area
  71.                  * where input event has been sent */
  72. SHORT         error;
  73.  
  74. struct IOStdReq *game_io_msg;
  75.  
  76. BYTE         gamebuffer[sizeof( struct InputEvent )];
  77. BYTE         *gamebuff;
  78.  
  79. SHORT         testval;
  80. SHORT         codevalue;
  81.     
  82. struct MsgPort     *game_msg_port;
  83.  
  84. SHORT movesize;
  85. extern struct MsgPort *CreatePort();
  86. extern struct IOStdReq *CreateStdIO();        
  87.  
  88. SHORT codeval, timeouts;
  89.  
  90. #define IF_NOT_IDLE_TWO_MINUTES while(timeouts < 4)
  91.  
  92. main()
  93. {
  94.     printf(" Joystick Demo\n");
  95.     printf("\nPlug a Joystick Into Right Port\n");
  96.     printf("\nThen move the stick and click its buttons");
  97.  
  98.     
  99.     gamebuff = &gamebuffer[0];    
  100.         /* point to first location in game buffer */
  101.  
  102. /* SYSTEM DEVICE COMMUNICATIONS SUPPORT SETUP ROUTINES *************** */
  103.  
  104.     game_msg_port = CreatePort(0,0);    
  105.         /* provide a port for the IO response */
  106.     if(game_msg_port == 0)
  107.         {
  108.         printf("\nError While Performing CreatePort");
  109.         exit(-1);
  110.         }
  111.  
  112.     game_io_msg = CreateStdIO(game_msg_port);     
  113.         /* make an io request block for communicating with
  114.                 the gameport */
  115.  
  116.     if(game_io_msg == 0)
  117.         {
  118.         printf("\nError While Performing CreateStdIO");
  119.         DeletePort(game_msg_port);
  120.         exit(-2);
  121.         }
  122. /* ********************************************************************** */
  123. /* OPEN THE DEVICE */
  124.  
  125.     error = OpenDevice("gameport.device",1,game_io_msg,0);
  126.         /* open the device for access, unit 1 is right port */
  127.  
  128.     if(error != 0)
  129.         {
  130.         printf("\nError while opening the device, exiting");
  131.         DeleteStdIO(game_io_msg);
  132.         DeletePort(game_msg_port);
  133.         exit(-3);
  134.         }
  135. /* ********************************************************************** */
  136. /* SET THE DEVICE TYPE */
  137.  
  138.     game_data = (struct InputEvent *)gamebuffer;
  139.  
  140.         /* test the joystick in this loop */
  141.     
  142.     if (set_controller_type(GPCT_ABSJOYSTICK) != 0)
  143.         {
  144.         printf("\nError while trying to set GPCT_ABSJOYSTICK");
  145.         DeleteStdIO(game_io_msg);
  146.                 DeletePort(game_msg_port);
  147.                 exit(-4);
  148.                 }
  149. /* ********************************************************************** */
  150. /* SET THE DEVICE TRIGGER */
  151.     if (set_controller_trigger() != 0)
  152.         {
  153.         printf("\nError while trying to set controller trigger");
  154.         DeleteStdIO(game_io_msg);
  155.                 DeletePort(game_msg_port);
  156.                 exit(-4);
  157.                 }
  158. /* ********************************************************************** */
  159. /* TELL USER WHAT YOU WILL BE DOING */
  160.  
  161.     printf("\nI will report: \n");
  162.     printf("\n     Stick X or Y moves");
  163.     printf("\n     Button presses (along with stick moves if any)");
  164.  
  165. /* ********************************************************************** */
  166. /* SETUP THE IO MESSAGE BLOCK FOR THE ACTUAL DATA READ */
  167.  
  168.     game_io_msg->io_Command = GPD_READEVENT;    
  169.         /* from now on, just read input events */
  170.     game_io_msg->io_Data = (APTR)gamebuffer;        
  171.         /* into the input buffer, one at a time. */
  172.         /* read-event waits for the preset conditions */
  173.     game_io_msg->io_Length = sizeof(struct InputEvent);    
  174.         /* read one event each time we go back to the gameport */
  175.     game_io_msg->io_Flags = 0;
  176.         /* dont use quick io */
  177.  
  178. /* ********************************************************************** */
  179. /* LOOP FOREVER */
  180.  
  181.     FOREVER
  182.     {
  183.     game_io_msg->io_Length = sizeof(struct InputEvent);    
  184.         /* read one event each time we go back to the gameport */
  185.  
  186.     printf("\n Waiting For Joystick Report\n");
  187.     SendIO(game_io_msg);
  188.     WaitPort(game_msg_port);
  189.         /* this is NOT a busy wait... it is a task-sleep */
  190.     GetMsg(game_msg_port);
  191.  
  192.     codevalue = game_data->ie_Code;
  193.  
  194.     if(codevalue == IECODE_LBUTTON) 
  195.         printf(" \nFire Button pressed");
  196.     if(codevalue == (IECODE_LBUTTON + IECODE_UP_PREFIX))
  197.         printf(" \nFire Button released");
  198.  
  199.     which_direction();    
  200.     showbugs();
  201.     }
  202.  
  203. /* PROGRAM EXIT ..... temporarily no way to get here from FOREVER */ 
  204.     set_controller_type(GPCT_NOCONTROLLER);
  205.  
  206.     CloseDevice(game_io_msg);
  207.     DeleteStdIO(game_io_msg);
  208.     DeletePort(game_msg_port);
  209.     
  210.     printf("\nExiting program... 2 minutes with no activity sensed\n1> ");
  211.     return(0);
  212. }        
  213.  
  214. int which_direction()        
  215. {
  216.     SHORT xmove, ymove;
  217.     xmove = game_data->ie_X;
  218.     ymove = game_data->ie_Y;
  219.  
  220.     switch(ymove) 
  221.         {
  222.         case (-1):
  223.             printf(" \nForward");
  224.             break;
  225.         case (1):
  226.             printf(" \nBack");
  227.             break;
  228.         default:
  229.             break;
  230.         }
  231.     switch(xmove) 
  232.         {
  233.         case (-1):
  234.             printf(" \nLeft");
  235.             break;
  236.         case (1):
  237.             printf(" \nRight");
  238.             break;
  239.         default:
  240.             break;
  241.         }
  242.     return(0);
  243. }
  244.  
  245. int set_controller_type(type)
  246. SHORT type;
  247. {
  248.     game_io_msg->io_Command = GPD_SETCTYPE;   
  249.         /* set type of controller to mouse */
  250.     game_io_msg->io_Length = 1;
  251.     game_io_msg->io_Data = (APTR)gamebuff;
  252.     *gamebuff = type;    
  253.  
  254.     SendIO(game_io_msg);    
  255.         /* set it up */
  256.         /* this command doesn't wait... returns immediately */
  257.     WaitPort(game_msg_port);
  258.     GetMsg(game_msg_port);
  259.     return((int)game_io_msg->io_Error);
  260. }
  261.  
  262. int set_controller_trigger()
  263. {
  264.     struct GamePortTrigger gpt;
  265.  
  266.     game_io_msg->io_Command = GPD_SETTRIGGER;   
  267.     game_io_msg->io_Length = sizeof(gpt);
  268.     game_io_msg->io_Data = (APTR)&gpt;
  269.     gpt.gpt_Keys = GPTF_UPKEYS+GPTF_DOWNKEYS;
  270.     gpt.gpt_Timeout = 0;
  271.     gpt.gpt_XDelta = 1;
  272.     gpt.gpt_YDelta = 1;
  273.  
  274.     return(DoIO(game_io_msg));
  275. }
  276.  
  277. showbugs()
  278. {
  279.     struct InputEvent *e;
  280.  
  281.     e = (struct InputEvent *)&gamebuffer[0];
  282.     /* where the input event gets placed */
  283.     printf("\nie_Class = %lx",e->ie_Class);
  284.     printf("\nie_SubClass = %lx",e->ie_SubClass);
  285.     printf("\nie_Code = %lx", e->ie_Code);
  286.     printf("\nie_Qualifier = %lx",e->ie_Qualifier);
  287.     printf("\nie_X = %ld", e->ie_X);
  288.     printf("\nie_Y = %ld", e->ie_Y);
  289.     printf("\nie_TimeStamp(seconds) = %lx", e->ie_TimeStamp.tv_secs);
  290.     return(0);
  291. }    
  292.  
  293.